import { reactive } from 'vue';
import { USER_ACCESS_TOKEN } from '@/libs/config.js';
// import { getUserInfoApi, getCustomerInfoApi } from '@/api/user';
// import { setStorageUserInfo } from '@/utils/utils.js';

/**
 * @description: 公共分页
 * @return {object}
 */
export function usePage(options = {}) {
  return reactive({
    page: 1,
    limit: 10,
    isLoadAll: false,
    ...options,
  });
}

// 拨打电话
export function useCallPhone(phoneNumber) {
  if (!phoneNumber) {
    uni.showToast({
      title: '电话号码为空',
      icon: 'none',
    });
    return;
  }
  uni.makePhoneCall({
    phoneNumber,
  });
}

// 用户身份信息
export function useUserIdentity() {
  const token = uni.getStorageSync(USER_ACCESS_TOKEN) || '';
  const userInfo = uni.getStorageSync('userInfo') || {};
  // try {
  //   if (token) {
  //     const { code, data } = await getUserInfoApi();
  //     if (code == 200) {
  //       const {
  //         code: code2,
  //         data: { list = [] },
  //       } = await getCustomerInfoApi();
  //       if (code2 == 200) {
  //         data.customer_id = list?.[0]?.id ?? '';
  //       }
  //       setStorageUserInfo(data);
  //     }
  //     console.log('--------------');
  //   }
  // } finally {
  //   userInfo = uni.getStorageSync('userInfo') || {};
  //   console.log('============');
  // }
  // console.log('+++++++++++++');
  return reactive({
    isLogin: Boolean(token),
    isCustomer: Boolean(token && userInfo?.customer_id),
  });
}
